package com.smalluz.ad.modules.ks.api.impl;

import java.util.Map;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.smalluz.ad.modules.common.config.MediaConfigStorage;
import com.smalluz.ad.modules.common.exception.ApiException;
import com.smalluz.ad.modules.common.tools.OkHttpUtils;
import com.smalluz.ad.modules.common.util.MediaConfigStorageHolder;
import com.smalluz.ad.modules.ks.api.KsCampaignService;
import com.smalluz.ad.modules.ks.api.KsClueService;
import com.smalluz.ad.modules.ks.api.KsReportService;
import com.smalluz.ad.modules.ks.api.KsService;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author biteam
 * @Title: BaseKsServiceImpl
 * @Description: TODO(描述)
 * @site http://www.smalluz.com/
 * @email 510711793@qq.com
 * @date 2022-06-21 20:37
 */
@Slf4j
public abstract class BaseKsServiceImpl implements KsService {

    @Getter
    @Setter
    private KsClueService ksClueService = new KsClueServiceImpl(this);

    @Getter
    @Setter
    private KsReportService ksReportService = new KsReportServiceImpl(this);
    
    @Getter
    @Setter
    private KsCampaignService ksCampaignService = new KsCampaignServiceImpl(this);
    
    @Getter
    @Setter
    private OkHttpUtils okHttpUtils = new OkHttpUtils();

    private Map<String, MediaConfigStorage> configStorageMap;

    @Override
    public KsService switchoverTo(String advertiserId) {
        if (this.configStorageMap.containsKey(advertiserId)) {
            MediaConfigStorageHolder.set(advertiserId);
            return this;
        }

        log.info("~~~~切换失败的授权ID记录advertiserId： {}",advertiserId);
        throw new ApiException("切换失败...");
    }

    @Override
    public MediaConfigStorage getMediaConfigStorage() {
        if (this.configStorageMap.size() == 1) {
            //如果只有一个账号的情况下直接返回属性
            return this.configStorageMap.values().iterator().next();
        }

        return this.configStorageMap.get(MediaConfigStorageHolder.get());
    }

    @Override
    public void setMultiConfigStorages(Map<String, MediaConfigStorage> configStorages) {
        this.setMultiConfigStorages(configStorages, configStorages.keySet().iterator().next());
    }

    @Override
    public void setMultiConfigStorages(Map<String, MediaConfigStorage> configStorages, String defaultAdId) {
        this.configStorageMap = Maps.newHashMap(configStorages);
        MediaConfigStorageHolder.set(defaultAdId);
    }

    @Override
    public void setConfigStorage(MediaConfigStorage configProvider) {
        final String defaultAdId = configProvider.getAdvertiserId();
        if (defaultAdId == null) {
            throw new ApiException("advertiserId不能设置为null");
        }

        this.setMultiConfigStorages(ImmutableMap.of(defaultAdId, configProvider), defaultAdId);
    }

    @Override
    public void addConfigStorage(String advertiserId, MediaConfigStorage configStorages) {
        synchronized (this) {
            if (this.configStorageMap == null) {
                this.setConfigStorage(configStorages);
            } else {
                MediaConfigStorageHolder.set(advertiserId);
                this.configStorageMap.put(advertiserId, configStorages);
            }
        }
    }
    
    protected String postNoToken(String path, String jsonStr) {
        return this.okHttpUtils.post(path, jsonStr);
    }
    
    protected String post(String accessToken,String path, String jsonStr) {
        return this.okHttpUtils.postKs(accessToken,path, jsonStr);
    }

    protected String get(String accessToken, String path, String jsonStr) {
        return this.okHttpUtils.get(accessToken, path, jsonStr);
    }
}
